package com.hdit.action.shop;

import java.util.Set;

import javax.annotation.Resource;

import com.hdit.entity.Auction;
import com.hdit.entity.AuctionApply;
import com.hdit.entity.OrderItem;
import com.hdit.entity.Product;
import com.hdit.entity.Order.OrderStatus;
import com.hdit.service.AuctionService;
import com.hdit.service.ProductService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 后台Action类 - 商品
 * ============================================================================
 */

@ParentPackage("shop")
public class AuctionAction extends BaseShopAction {

	private static final long serialVersionUID = -4433964283757192334L;
	
	private Product product;
	
	private Auction auction;

	@Resource
	private ProductService productService;
	
	@Resource
	private AuctionService auctionService;
	
	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		auction = auctionService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Auction.class);
		detachedCriteria.add(Restrictions.eq("member", getLoginMember()));
		
		
		System.out.println("getLoginMember()========"+getLoginMember().getId());
		pager = auctionService.findByPager(pager,detachedCriteria);
		return LIST;
	}

	// 删除
	public String delete() throws Exception {
		for (String id : ids) {
			Product product = productService.load(id);
			Set<OrderItem> orderItemSet = product.getOrderItemSet();
			for (OrderItem orderItem : orderItemSet) {
				if (orderItem.getOrder().getOrderStatus() != OrderStatus.completed && orderItem.getOrder().getOrderStatus() != OrderStatus.invalid) {
					return ajaxJsonErrorMessage("商品[" + product.getName() + "]订单处理未完成，删除失败！");
				}
			}
		}
		productService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}
	
	
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		
		try{
		
		Auction persistent = auctionService.load(id);
		
		persistent.setStatus(auction.getStatus());
		
		auctionService.update(persistent);
		
		redirectionUrl = "auction!list.action";
		return SUCCESS;
		
		}
		
		catch(Exception e){
			System.out.println("===============================");
			e.printStackTrace();
			throw e;
		}
	}


	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Auction getAuction() {
		return auction;
	}

	public void setAuction(Auction auction) {
		this.auction = auction;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public AuctionService getAuctionService() {
		return auctionService;
	}

	public void setAuctionService(AuctionService auctionService) {
		this.auctionService = auctionService;
	}

}